package cn.bjit.controller;

import cn.bjit.request.CartItemRequest;
import cn.bjit.service.CartService;
import cn.bjit.util.JsonData;
import cn.bjit.vo.CartVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Api(tags = "购物车模块")
@RestController
@RequestMapping("/api/cart/v1")
public class CartController {
    @Resource
    private CartService cartService;
    @ApiOperation("添加购物车")
    @PostMapping("/add")
    public JsonData addToCart(@ApiParam("购物车")@RequestBody CartItemRequest cartItemRequest){
        cartService.addToCart(cartItemRequest);
        return JsonData.buildSuccess();
    }
    @ApiOperation("查看购物车")
    @GetMapping("/findMyCart")
    public JsonData findMyCart(){
        CartVO cartVo= cartService.getMyCart();
        return JsonData.buildSuccess(cartVo);
    }
    //删除购物车
    @ApiOperation("删除购物车")
    @DeleteMapping("/delete/{product_id}")
    public JsonData deleteItem(@ApiParam(value = "商品id",required = true)@PathVariable("product_id")long productId){
        cartService.deletItem(productId);
        return JsonData.buildSuccess();
    }

    //清空购物车
    @ApiOperation("清空购物车")
    @DeleteMapping("/clear")
    public JsonData clearMyCart(){
       cartService.clear();
       return JsonData.buildSuccess();
    }

    //修改购物车
    @ApiOperation("修改购物车")
    @DeleteMapping("/change")
    public JsonData changeItemNum(@ApiParam("购物项")@RequestBody CartItemRequest cartItemRequest){
        cartService.changeItemNum(cartItemRequest);
        return JsonData.buildSuccess();
    }

}
